package com.aptemo.webetek.client.widgets.payments;

import java.util.ArrayList;
import java.util.List;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.impl.GetPaymentsAction;
import com.aptemo.webetek.client.commands.responses.GotPaymentsResponse;
import com.aptemo.webetek.client.enums.EnumPaymentType;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.widgets.paging.PagingController;
import com.aptemo.webetek.shared.PaymentBean;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;

public class PaymentsPresenter
		extends PagingController<PaymentBean>
{

	private HandlerManager				meventbus;
	private WebetekUserServiceAsync		mservice;
	protected boolean					misAdmin	= false;
	private PaymentsDisplay				display;
	private int							pageNo		= 0;
	protected ArrayList<PaymentBean>	list;

	private EnumPaymentType				type		= EnumPaymentType.PAYMENT;
	private String						teamId;

	public PaymentsPresenter(PaymentsDisplay view, HandlerManager eventbus, WebetekUserServiceAsync userService, String id)
	{
		this(view, eventbus, userService, id, 0);
	}

	public PaymentsPresenter(PaymentsDisplay view, HandlerManager eventbus, WebetekUserServiceAsync userService, String id, int pageNo)
	{
		super(view.getPagingView(), 5);

		this.meventbus = eventbus;
		this.mservice = userService;
		this.display = view;
		this.pageNo = pageNo;
		this.teamId = id;
	}

	private void bind()
	{
		// bind paging
		// display.getPagingView().setVisible(!hidePaging);

		display.setStatusFilterSelected(type);

		display.getList().addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				int row = display.getClickedRow(event);
				PaymentBean stake = getItem(row);
			}
		});

		display.getStatusFilter().addChangeHandler(new ChangeHandler()
		{

			@Override
			public void onChange(ChangeEvent event)
			{
				type = display.getStatusFilterValue();

				renderList();
			}
		});
	}

	private void retrieveList()
	{
		display.getInfoField().setText(display.translate("loading"));

		GetPaymentsAction action = new GetPaymentsAction(teamId);
		mservice.invoke(action, new GotPaymentsResponse()
		{

			@Override
			public void got(ArrayList<PaymentBean> list)
			{
				// display.getInfoField().setText("Payments list [" +
				// list.size() + "]");
				display.getInfoField().setText("");
				PaymentsPresenter.this.list = list;
				renderList();
			}

			@Override
			public void onError(String message, EErrorCodes code)
			{
				display.getInfoField().setText(message);
			}

		});
	}

	protected void renderList()
	{
		ArrayList<PaymentBean> payments = new ArrayList<PaymentBean>();
		for (PaymentBean payment : list)
		{
			if (type == null || type.equals(payment.getType()))
			{
				payments.add(payment);
			}
		}
		setList(payments, 0);

	}

	@Override
	public void go(HasWidgets container)
	{
		container.clear();
		container.add(display.getDisplay());
		bind();

		retrieveList();
	}

	@Override
	public void refreshPaging(List<PaymentBean> data)
	{
		display.updatePaymentList(data);
	}

	public void setPageNo(int pageNo)
	{
		this.pageNo = pageNo;
	}

}
